/* * * * M A P V I S I O N   L I C E N S E  *****************************************************************
 *
 * This MapVision software is © Filip Zavadil 2013. It is licensed under the LGPL license.
 * This MapVision software dynamically links to unmodified Qt5 Library.
 *
 * The Qt5 Library is © 2013 Qt® and/or its subsidiary(-ies), and is licensed under the
 * GNU Lesser General Public License version 2.1 with Qt® LGPL exception version 1.1.
 * Qt5 library is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
 * Qt5 library is provided “AS IS”, without WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * As an additional permission to the GNU Lesser General Public License version 2.1, the object
 * code form of a “work that uses the Library” may incorporate material from a header file that is
 * part of the Library. You may distribute such object code under terms of your choice,
 * provided that: (i) the header files of the Library have not been modified; and (ii)
 * the incorporated material is limited to numerical parameters, data structure layouts, accessors,
 * macros, inline functions and templates; and (iii) you comply with the terms of Section 6 of the
 * GNU Lesser General Public License version 2.1.
 *
 * Moreover, you may apply this exception to a modified version of the Library, provided that such
 * modification does not involve copying material from the Library into the modified Library’s header
 * files unless such material is limited to (i) numerical parameters; (ii) data structure layouts;
 * (iii) accessors; and (iv) small macros, templates and inline functions of five lines or less in length.
 *
 * Furthermore, you are not required to apply this additional permission to a modified version of the Library.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this package;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

 * The source code for Qt 5.X SDK is available from Qt Project here:
 * http://download.qt-project.org/official_releases/qtcreator/2.7/2.7.0/qt-creator-2.7.0-src.zip.
 *
 * ****************************** M A P V I S I O N    S O F T W A R E  ************************************
 *
 * Author:       Filip Zavadil
 * Description:  Software for displaying and exporting map tiles.
 * The sources and executables of MapVision software are available:
 *    - from project hosting on CodePlex.com :   http://mapvision.codeplex.com
 *    - from GIT repositories on Gitorious.org:  https://gitorious.org/mapvision
 * Visit MapVision fanpage on Facebook.com:      https://www.facebook.com/pages/Mapvision/631827753513935
 *
 * *********************************************************************************************************
*/

#include "dockwidgeteffects.h"
#include "ui_dockwidgeteffects.h"

DockWidgetEffects::DockWidgetEffects(MapVision *parent) :
    DockWidgetGeneral(parent),
    ui(new Ui::DockWidgetEffects)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("Graphical Effects"));
    ui->comboBox->addItem(tr("... no effect","noEffect"));
    ui->comboBox->addItem(tr("gaussian blur","blur"));
    ui->comboBox->addItem(tr("colorize effect","colorize"));
    ui->comboBox->addItem(tr("drop shadow effect","shadow"));

    colorDialogColorize = new QColorDialog(this);
    colorDialogColorize->setCurrentColor(Qt::green);
    colorDialogDropShadow = new QColorDialog(this);
    colorDialogDropShadow->setCurrentColor(Qt::green);

    connect(colorDialogDropShadow,SIGNAL(currentColorChanged(QColor)),this,SLOT(colorHasSelected(QColor)));
    connect(colorDialogColorize,SIGNAL(currentColorChanged(QColor)),this,SLOT(colorHasSelected(QColor)));
    connect(getMapView(),SIGNAL(itemSelected(QGraphicsItem*)),this,SLOT(itemWasSelected(QGraphicsItem*)));

    // more effects on http://www.slideshare.net/qtbynokia/special-effects-with-qt-graphics-view#btnNext
     this->hide();
}

DockWidgetEffects::~DockWidgetEffects()
{
    delete ui;
}

void DockWidgetEffects::on_comboBox_currentIndexChanged(int index)
{
    ui->stackedWidget->setCurrentIndex(index);
    ui->widgetEffect->setEnabled(index!=0);

}

void DockWidgetEffects::applyBlurEffect()
{
    foreach (QGraphicsItem * item, getMapView()->getScene()->selectedItems()){
        QGraphicsBlurEffect *effect = new QGraphicsBlurEffect;
        effect->setBlurHints(QGraphicsBlurEffect::QualityHint);
        effect->setObjectName("blur");
        effect->setBlurRadius(ui->doubleSpinBoxBlur->value());
        item->setGraphicsEffect(effect);
    }
}


void DockWidgetEffects::applyColorizeEffect()
{
    foreach (QGraphicsItem * item, getMapView()->getScene()->selectedItems()){
        QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect;
        effect->setObjectName("colorize");
        effect->setColor(colorDialogColorize->currentColor());
        effect->setStrength(ui->doubleSpinBox->value());
        item->setGraphicsEffect(effect);
    }
}

void DockWidgetEffects::applyDropShadowEffect()
{
    foreach (QGraphicsItem * item, getMapView()->getScene()->selectedItems()){
        QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
        effect->setObjectName("shadow");
        effect->setColor(colorDialogDropShadow->currentColor());
        effect->setXOffset(ui->spinBoxDropShadowOffsetX->value());
        effect->setYOffset(ui->spinBoxDropShadowOffsetY->value());
        effect->setBlurRadius(ui->spinBoxDropShadowRadius->value());
        item->setGraphicsEffect(effect);
    }
}

void DockWidgetEffects::on_toolButtonRemoveEfect_clicked()
{
    foreach (QGraphicsItem * item, getMapView()->getScene()->selectedItems()){
        item->setGraphicsEffect(NULL);
    }
}

void DockWidgetEffects::on_toolButtonApply_clicked()
{
    QString value = ui->comboBox->itemData(ui->comboBox->currentIndex()).toString();

    if (value=="shadow"){
        foreach (QGraphicsItem * item, getMapView()->getScene()->selectedItems()){
            ui->widgetEffect->setEnabled(true);
            applyDropShadowEffect();
            Q_UNUSED(item)
        }
    } else if (value=="blur"){
        foreach (QGraphicsItem * item, getMapView()->getScene()->selectedItems()){
            ui->widgetEffect->setEnabled(true);
            applyBlurEffect();
            Q_UNUSED(item)
        }
    } else if (value=="colorize"){
        foreach (QGraphicsItem * item, getMapView()->getScene()->selectedItems()){
            ui->widgetEffect->setEnabled(true);
            applyColorizeEffect();
            Q_UNUSED(item)
        }
    } else {
        QGraphicsEffect * effect = NULL;
        ui->widgetEffect->setEnabled(false);
    }
}

void DockWidgetEffects::on_toolButtonColorPalette_clicked()
{
    QColor color = colorDialogColorize->getColor(Qt::black, this, "Text Color",  QColorDialog::DontUseNativeDialog);
    if(color.isValid()){
        colorDialogColorize->setCurrentColor(color);
        applyColorizeEffect();
    }
}

void DockWidgetEffects::on_toolButtonDropShadowColor_clicked()
{
    QColor color = colorDialogDropShadow->getColor(Qt::black, this, "Text Color",  QColorDialog::DontUseNativeDialog);
    if(color.isValid()){
        colorDialogDropShadow->setCurrentColor(color);
        applyDropShadowEffect();
    }
}


void DockWidgetEffects::on_spinBoxDropShadowRadius_valueChanged(int arg1)
{ 
    applyDropShadowEffect();
}

void DockWidgetEffects::on_spinBoxDropShadowOffsetX_valueChanged(int arg1)
{
   applyDropShadowEffect();
}

void DockWidgetEffects::on_spinBoxDropShadowOffsetY_valueChanged(int arg1)
{   
  applyDropShadowEffect();
}

void DockWidgetEffects::colorHasSelected(QColor color)
{

    ui->toolButtonDropShadowColor->setStyleSheet("background-color: "+colorDialogDropShadow->currentColor().name()+";");
    ui->toolButtonColorPalette->setStyleSheet("background-color: "+colorDialogColorize->currentColor().name()+";");
}

void DockWidgetEffects::itemWasSelected(QGraphicsItem *item)
{
    if (item->graphicsEffect()==NULL){
        ui->widgetEffect->setEnabled(false);
        ui->comboBox->setCurrentIndex(0);
    } else {
        ui->widgetEffect->setEnabled(true);
        int i=ui->comboBox->findData(item->graphicsEffect()->objectName());
        ui->comboBox->setCurrentIndex(i);
    }
}

void DockWidgetEffects::on_doubleSpinBoxBlur_valueChanged(double arg1)
{
    applyBlurEffect();
}



void DockWidgetEffects::on_doubleSpinBox_valueChanged(double arg1)
{
    applyColorizeEffect();
}

void DockWidgetEffects::on_toolButtonGrayScale_clicked()
{
    ui->doubleSpinBox->setValue(1);
    colorDialogColorize->setCurrentColor(Qt::black);
    applyColorizeEffect();
}
